package test.day04;

import java.util.Stack;

public class EffectBrackets {
	public boolean isValid(String s) {
		for (int i = 0; i < s.length() / 2; i++) {
			s = s.replace("{}", "").replace("[]", "").replaceAll("()", "");
		}
		return s.isEmpty();
	}

	public boolean isValid1(String s) {
		Stack<Character> stack = new Stack();
		for (int i = 0; i < s.toCharArray().length; i++) {
			char c = s.charAt(i);
			if (c == '(') {
				stack.push(')');
			} else if (c == '{') {
				stack.push('}');
			} else if (c == '[') {
				stack.push(']');
			} else if (stack.isEmpty() || c != stack.pop()) {
				return false;
			}
		}
		return stack.isEmpty();
	}
}
